package servlet;

import dao.AttendanceDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet("/one_page_attendance")
public class one_page_attendance extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String teacher_id;
        if(request.getParameter("teacher_id") != null) {
            teacher_id = request.getParameter("teacher_id");
        }else{
            teacher_id = (String) session.getAttribute("teacher_id");
        }
        int currentIndex, count, size = 10;
        String index = request.getParameter("index");
        if(index == null) index = "1";
        currentIndex = Integer.parseInt(index);

            try{
                AttendanceDao attendanceDao = new AttendanceDao();
                Map<String, String> attendanceMap = attendanceDao.getCoursesByTeacher(teacher_id);
                count = attendanceMap.size();
                Map<String,String> attendanceMap1 = attendanceDao.getAttendanceOnePage(attendanceMap, currentIndex, size);
                int sumIndex = count % size == 0 ? count / size : count / size + 1;
                session.setAttribute("attendanceMap", attendanceMap1);
                session.setAttribute("sumIndex", sumIndex);
                response.sendRedirect("teacher/attendance.jsp");
            }catch (Exception e){
                out.print(e);
            }



    }
}
